package zj.com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.SelectAdapter;
import zj.com.adapter.SelectJcdAdapter;
import zj.com.db.DBUtil;
import zj.com.model.Dic;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/17.
 */
public class SelectJcdActivity extends  BaseActivity implements View.OnClickListener{


    public ListView listView;
    public TextView title_name;
    public ImageView left_iv;
    public int type;
    public SelectJcdAdapter adapter;

    public  String dicType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_jcd);
        initView();
        initData();
    }

    public void initView(){
        list = new ArrayList<Dic>();

        listView = (ListView) findViewById(R.id.listView);
        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("选择");


        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);
    }

    public void initData(){


        adapter = new SelectJcdAdapter(list,this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent();
                intent.putExtra("dic", list.get(position));
                setResult(RESULT_OK, intent);
                finish();

            }
        });
        dicType  = getIntent().getStringExtra("dicType");
        LogUtils.getInstance().error("dicType="+dicType);
        try {
            List<Dic> list = DBUtil.getInstance(this).getDBManager().selector(Dic.class).where("dic_type","=",dicType).findAll();


            if(list != null && list.size() > 0){
                initDataFromDB();
            }else{
                obtainDic();
            }
        } catch (DbException e) {
            LogUtils.getInstance().error(e.toString());
            e.printStackTrace();
        }
    }

    public void obtainDic(){

        NetManager.getInstance(this).obtainDic(dicType, new OnResponseListener<List<Dic>>() {
            @Override
            public void onComplete(boolean isOK, List<Dic> result, int errorCode, String errorMsg) {

                if(isOK){
                    initDataFromDB();
                }else{
                    Toast.makeText(SelectJcdActivity.this,"获取失败",Toast.LENGTH_LONG).show();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                Toast.makeText(SelectJcdActivity.this,"获取失败",Toast.LENGTH_LONG).show();
            }
        });

    }

    public  List<Dic> list;
    public void initDataFromDB(){
        try {
            list   = DBUtil.getInstance(this).getDBManager().selector(Dic.class).where("dic_type","=",dicType).findAll();
           if(list != null){
               adapter.setDataChange(list);
           }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
        }
    }
}
